package fun.werfamily.wf.order.core.domain.remote.facade;

import com.alibaba.fastjson.JSON;
import fun.werfamily.wf.base.vo.Result;
import fun.werfamily.wf.order.core.domain.dto.req.goods.GoodsDetailReqDTO;
import fun.werfamily.wf.order.core.domain.dto.req.goods.GoodsSkuDTO;
import fun.werfamily.wf.order.core.domain.dto.req.goods.GoodsSkuDetailReqDTO;
import fun.werfamily.wf.order.core.domain.dto.result.goods.GoodsSpuDTO;
import fun.werfamily.wf.order.core.domain.remote.service.GoodsRemoteService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Mr.WenMing Zhang
 * @Date: 2022-08-28 17:33
 * @Description:
 */
@Component
@Slf4j
public class GoodsServiceFacade {
    @Resource
    private GoodsRemoteService goodsRemoteService;

    /**
     * 获取商品spu详情
     * @param spuCode
     * @return
     */
    public GoodsSpuDTO goodsSpuDetail(String spuCode){
        GoodsDetailReqDTO goodsDetailReqDTO = new GoodsDetailReqDTO();
        goodsDetailReqDTO.setSpuCode(spuCode);
        try {
            Result<GoodsSpuDTO> result = goodsRemoteService.goodsSpuDetail(goodsDetailReqDTO);
            log.info("调用goods-core获取商品详情,req={},resp={}", JSON.toJSONString(goodsDetailReqDTO), JSON.toJSONString(result));
            if (result.isSuccess()) {
                return result.getData();
            }
        } catch (Exception e) {
            log.error("调用goods-core获取商品详情，异常req={}", JSON.toJSONString(goodsDetailReqDTO), e);
        }
        return new GoodsSpuDTO();
    }

    /**
     * 获取商品详情
     * @param skuCodes
     * @return
     */
    public List<GoodsSkuDTO> goodsSkuDetail(List<String> skuCodes){
        GoodsSkuDetailReqDTO goodsSkuDetailReqDTO = new GoodsSkuDetailReqDTO();
        goodsSkuDetailReqDTO.setSkuCodes(skuCodes);
        try {
            Result<List<GoodsSkuDTO>> result = goodsRemoteService.goodsSkuDetail(goodsSkuDetailReqDTO);
            log.info("调用goods-core批量获取商品sku详情,req={},resp={}", JSON.toJSONString(goodsSkuDetailReqDTO), JSON.toJSONString(result));
            if (result.isSuccess()) {
                return result.getData();
            }
        } catch (Exception e) {
            log.error("调用goods-core批量获取商品sku详情，异常req={}", JSON.toJSONString(goodsSkuDetailReqDTO), e);
        }
        return new ArrayList<>();
    }
}
